import json
from email.policy import default

from fastapi import APIRouter, Depends
from requests import Session

from Util.ActivityUtil import get_all_sign_activity
from Util.AuthUtil import get_token
from config.Database import get_db
from config.Env import SystemUserInfo
from models.UserInfoModel import UserInfo
from schemas.ClockRecord import ClockRecordRequest
from schemas.Result import Result
from schemas.UserInfo import UserInfoRequest, TokenRequest
from services.ClockRecordServe import ClockRecordServe
from services.UserService import UserServe

router = APIRouter(
    prefix="/user",
    tags=["user"],
    responses={404: {"description": "Not found"}},
)

UserServe = UserServe()

@router.post("/history")
async def get_history(clockRecord: ClockRecordRequest,db: Session = Depends(get_db)):
    res = await UserServe.getUserHistory(clockRecord)
    if res.get('code') == 0:
        return Result.success(data=res.get('data'))
    else: Result.fail(msg=res.get('msg'))


# 通过token 获取用户可以签到的活动列表
@router.post("/list")
async def get_user_sign_activity(token:TokenRequest,db: Session = Depends(get_db)):
    if not token.token:
        return Result.fail(msg="参数错误")
    res = await get_all_sign_activity(token.token)
    return Result.success(res.get('data')['list'])


# 通过账号密码 获取用户可以签到的活动列表
@router.post("/activity")
async def get_user_sign_activity(userinfo:UserInfoRequest, db: Session = Depends(get_db)):
    token = await get_token(userinfo.username,userinfo.password)
    if not token:
        return Result.fail(msg="账号或密码错误")
    
    allUserInfo = await UserServe.save_user_info(token,db)
    res = await get_all_sign_activity(token)
    if not allUserInfo:
        return Result.fail(msg="获取用户信息失败")
    return Result.success(data={
        "userid": allUserInfo.id,
        "token":token,
        "list":res.get('data')['list']
    })